package com.userdemo;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class UserLog {
private static File file = new File("/Users/edy/Desktop/file/1.txt");

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名");
        String name = scanner.nextLine();
        System.out.println("输入密码");
        String password = scanner.nextLine();

        try {
            if (log(name,password)) {
                System.out.println("恭喜" + name + "登录成功");
            } else if (!(exist(name))) {
                System.out.println("账号不存在，请输入y来注册账号");
                String yorn = scanner.nextLine();
                if (yorn.equals("y")) {
                    System.out.println("请输入注册的账号");
                    String registerName = scanner.nextLine();
                    System.out.println("请输入注册的密码");
                    String registerPassword = scanner.nextLine();
                    while (exist(registerName)) {
                        System.out.println("账号重复，请重新注册");
                        System.out.println("请输入注册的账号");
                        registerName = scanner.nextLine();
                        System.out.println("请输入注册的密码");
                        registerPassword = scanner.nextLine();
                    }
                    System.out.println("注册成功");
                    save(registerName,registerPassword);

                } else {
                    System.out.println("由于你没有输入y，已经自动退出程序");
                }


            } else {
                System.out.println("账号存在，密码错误，请输入y重置密码");
                String yorn = scanner.nextLine();
                if (yorn.equals("y")) {
                    System.out.println("请第一次输入要修改的密码");
                    String uppassword = scanner.nextLine();
                    System.out.println("请再输一次密码");
                    String secondPassword = scanner.nextLine();
                    if (secondPassword.equals(uppassword)) {
                        System.out.println("修改密码成功");
                        update(name,uppassword);
                    } else {
                        System.out.println("对不起，由于您输入的二次密码不一样，已经退出");
                    }
                } else {
                    System.out.println("由于你没有输入y，已经自动退出程序");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }




    public static List<User> getallUser() throws IOException {
        File file = new File("/Users/edy/Desktop/file/1.txt");
        List<String> userList = FileUtils.readLines(file, "UTF-8");
        List<User> users = new ArrayList<>();
        for (String s : userList) {
            String[] split = s.split(",");
            User user = new User(split[0],split[1]);
            users.add(user);
        }
        return users;
    }

    public static boolean log(String username, String userpassword) throws IOException {
        //  1。登陆是否成功
        boolean flag = false;
        List<User> users = getallUser();
        for (User user : users) {
            if (user.getName().equals(username) && user.getPassword().equals(userpassword)) {
                flag = true;
            }
        }
        return flag;
    }


    // 2.账号是否存在
    public static boolean exist(String username) throws IOException {
        List<User> users = getallUser();
        boolean flag = false;
        for (User user : users) {
            if (user.getName().equals(username)) {
                flag = true;
            }
        }
        return flag;
    }

    // 3.存储账号
    public static void save(String username, String password) throws IOException {
        FileUtils.writeStringToFile(file,username+","+password+"\n","UTF-8",true);

    }

    //4.重置密码
    public static void update(String username, String newpassword) throws IOException {
        List<User> users = getallUser();
        FileUtils.writeStringToFile(file,"","UTF-8");
        for (User user : users) {
            if ((user.getName().equals(username))) {
                user.setPassword(newpassword);
            }
            FileUtils.writeStringToFile(file,user.getName()+","+user.getPassword()+"\n",true);
        }
    }

}
